
import articles from "../model/article.js";
import { getLocalIP } from '../utils/index.js';
import { PORT } from "../config/index.js";
const IP = getLocalIP();





export const getArticleList = async (pageNum = 1, pageSize = 10) => {
    if (pageSize !== 10) {
        pageSize = 10;
    }
    if (pageNum == 1) {
        const sub = articles.slice(0, 10);
        return sub.map(item => {
            return {
                id: item.id,
                title: item.title,
                userName: item.userName,
                avatarUrl: `http://${IP}:${PORT}/public${item.avatarUrl}`,
                favoriteCount: item.favoriteCount,
                isFavorite: item.isFavorite,
                image: `http://${IP}:${PORT}/public${item.images[0]}`,
            };
        })
    } else if (pageNum == 2) {
        const sub = articles.slice(10, 20);
        return sub.map(item => {
            return {
                id: item.id,
                title: item.title,
                userName: item.userName,
                avatarUrl: `http://${IP}:${PORT}/public${item.avatarUrl}`,
                favoriteCount: item.favoriteCount,
                isFavorite: item.isFavorite,
                image: `http://${IP}:${PORT}/public${item.images[0]}`,
            };
        })
    } else {
        return [];
    }
};


export const getArticleDetail = async (id) => {
    const find = articles.find(i => i.id === id);
    if (!find) {
        return {};
    }
    const article = JSON.parse(JSON.stringify(find))
    article.avatarUrl = `http://${IP}:${PORT}/public${find.avatarUrl}`;
    article.images = article.images.map(i => `http://${IP}:${PORT}/public${i}`);
    article.comments = article.comments ? article.comments.map(i => {
        return {
            ...i,
            avatarUrl: `http://${IP}:${PORT}/public${i.avatarUrl}`
        };
    }) : [];
    if (article?.comments[0]?.children) {
        const firstCommentChildren = [...article.comments[0].children];
        if (firstCommentChildren?.length) {
            const copySubFirst = firstCommentChildren[0];
            copySubFirst.avatarUrl = `http://${IP}:${PORT}/public${copySubFirst.avatarUrl}`;
            firstCommentChildren[0] = copySubFirst;
        }
        article.comments[0].children = firstCommentChildren;
    }
    return article;
};
